-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
-- Copyright (C) 2012 societe Publica
-- 
-- Authors:
--   Matthew Wild
--   Waqas Hussain
--   Guilhem Lettron <guilhem.lettron@youscribe.com>
-- 
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--

local g_b     = module:require 'groups_bookmarks';

if not g_b then
	module:log("error", "Can't find groups_bookmarks lib");
	return;
end

function module.load()
	local bookmarks_file;
	bookmarks_file = module:get_option_string("group_bookmarks_file");

	if not bookmarks_file then
		module:log("error", "Please specify group_bookmarks_file in your configuration");
		return;
	end
	
	local curr_room;
	for line in io.lines(bookmarks_file) do
		if line:match("^%s*%[.-%]%s*$") then
			curr_room = line:match("^%s*%[(.-)%]%s*$");
			module:log("debug", "New group: %s", tostring(curr_room));
		elseif curr_room then
			-- Add JID
			local entryjid, name = line:match("([^=]*)=?(.*)");
			module:log("debug", "entryjid = '%s', name = '%s'", entryjid, name);
			local jid;
			jid = jid_prep(entryjid:match("%S+"));
			if jid then
				module:log("debug", "New member of %s: %s", tostring(curr_room), tostring(jid));
				g_b.add_user(jid, curr_room, name);
			end
		end
	end
	module:log("info", "Group bookmarks loaded successfully");
end

